package com.example.cart.controller;

import com.example.cart.domain.dto.CartDTO;
import com.example.cart.domain.vo.CartVO;
import com.example.cart.domain.vo.OrderPreVO;
import com.example.cart.service.CartService;
import com.example.common.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "购物车接口相关")
@Slf4j
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {
    private final CartService cartService;

    /**
     * 获取购物车信息
     *
     * @return
     */
    @Operation(summary = "获取购物车信息")
    @GetMapping
    public Result<List<CartVO>> getCart() {
        List<CartVO> cartList = cartService.getCart();
        return Result.success(cartList);
    }

    /**
     * 添加商品到购物车
     *
     * @return
     */
    @Operation(summary = "添加商品到购物车")
    @PostMapping("/add")
    public Result addCart(@Validated @RequestBody CartDTO cartDTO) {
        log.info("添加商品到购物车：{}", cartDTO);
        cartService.addCart(cartDTO);
        return Result.success();
    }

    /**
     * 删除购物车商品
     *
     * @return
     */
    @Operation(summary = "删除购物车商品")
    @DeleteMapping
    public Result deleteCart(@RequestBody Long ids) {
        log.info("删除购物车商品：{}", ids);
        List<Long> idList = new ArrayList<>();
        idList.add(ids);
        cartService.deleteCart(idList);
        return Result.success();
    }

    @Operation(summary = "修改购物车商品数量和选中")
    @PutMapping()
    public void updateCart( @RequestBody CartDTO cartDTO) {
        log.info("商品修改值：{}", cartDTO);
       cartService.updateCart( cartDTO);
    }
    @Operation(summary = "修改购物车商品选中状态")
    @PutMapping("/selected/{selected}")
    public void updateCartSelected(@PathVariable Integer selected) {
        log.info("商品要修改的状态：{}", selected);
        cartService.updateCartSelected(selected);
    }
    /**
     * 获取预付订单
     * @return
     */
    @Operation(summary = "获取预付订单")
    @GetMapping("/preOrder")
    public Result<OrderPreVO> preOrder() {
        log.info("获取预付订单");
        OrderPreVO orderPreResult =cartService.getPreOrder();
        return Result.success(orderPreResult);
    }

    /**
     * 获取立即购买预付订单
     * @param skuId
     * @param count
     * @return
     */
    @Operation(summary = "获取立即购买预付订单")
    @GetMapping("/preOrder/now")
    public Result<OrderPreVO> preOrderNow(Long skuId,Integer count) {
        log.info("获取立即购买预付订单");
        OrderPreVO orderPreResult =cartService.getPreOrderNow(skuId,count);
        return Result.success(orderPreResult);
    }


}


